<template>
    <div class="game-all-expand-box" v-loading="loading">
        <div class="table-box">
            <el-table
                :data="list"
                @sort-change="sortChange"
                border
                style="width: 100%">
                <el-table-column
                    min-width="120"
                    label="游戏名称">
                    <template slot-scope="scope">
                        <span @click="goGameDetailBlank(scope.row.gameId)" class="table-btn">{{scope.row.gameName || scope.row.gameId || '0'}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="渠道">
                    <template slot-scope="scope">
                        <div v-if="scope.row.source === 0">taptap</div>
                        <div v-else-if="scope.row.source === 1">应用宝</div>
                        <div v-else-if="scope.row.source === 2">客户</div>
                        <div v-else-if="scope.row.source === 3">学习识别</div>
                        <div v-else-if="scope.row.source === 4">人工</div>
                        <div v-else-if="scope.row.source === 5">小米</div>
                        <div v-else>-</div>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="today"
                    label="今日下载量">
                    <template slot-scope="scope">
                        <span>{{scope.row.today || '0'}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="todayStart"
                    label="今日启动量">
                    <template slot-scope="scope">
                        <span>{{scope.row.todayStart || '0'}}</span>
                        <span v-if="scope.row.todayUserStart">({{scope.row.todayUserStart}}人)</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="total"
                    label="累计下载量">
                    <template slot-scope="scope">
                        <span>{{scope.row.total || '0'}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="totalStart"
                    label="累计启动量">
                    <template slot-scope="scope">
                        <span>{{scope.row.totalStart || '0'}}</span>
                        <span v-if="scope.row.totalUserStart">({{scope.row.totalUserStart}}人)</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="todayPlayHour"
                    label="今日游戏时长">
                    <template slot-scope="scope">
                        <span v-if="scope.row.todayPlayHour">{{scope.row.todayPlayHour || '0'}}小时</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="totalPlayHour"
                    label="累计游戏时长">
                    <template slot-scope="scope">
                        <span v-if="scope.row.totalPlayHour">{{scope.row.totalPlayHour || '0'}}小时</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="tools">
            <!-- <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination> -->

            <el-pagination
                @current-change="pageChange"
                @size-change="sizeChange"
                :page-sizes="_pageSizes"
                :current-page="pageNum"
                layout="total,sizes, prev, pager, next"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
export default {
    data () {
        return {
            loading: false,
            list: [],
            pageNum: 1,
            pageSize: 10,
            total: 0,
            sort: '',
            type: ''
        }
    },

    props: ['time', 'source'],

    watch: {
        source () {
            this.getList(1)
        }
    },

    created () {
        this.getList(1)
    },

    methods: {
        async getList (num) {
            if (this.loading) return
            this.loading = true
            this.pageNum = num

            let params = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                fromDate: this.time,
                toDate: this.time,
                type: this.type,
                sort: this.sort
            }

            // 渠道
            if (this.source !== -1) {
                params.source = this.source
            }

            let res = await this.$http.get('/data/selectGameData', {
                params
            })

            if (res.code === '0') {
                this.list = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
            this.loading = false
        },

        // 去游戏详情
        goGameDetailBlank (id) {
            const { href } = this.$router.resolve({
                path: '/game/gameDetail',
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        },

        sizeChange (val) {
            this.pageSize = val
            this.getList(1)
        },

        pageChange (val) {
            this.getList(val)
        },

        // 后端排序
        sortChange ({ column, prop, order }) {
            this.sort = order || ''
            // 如果没有 sort prop 置为空
            if (!this.sort) {
                this.type = ''
            } else {
                this.type = prop
            }
            this.getList(1)
        }
    }
}
</script>

<style lang="scss" scoped>
    .game-all-expand-box {
        .table-box {
            > div {
                border-top: none;
            }
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
        }

        .tools {
            margin-top: 5px;
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
